package com.ttdj.cmel.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.ttdj.cmel.R;
import com.ttdj.cmel.activity.WebViewActivity;
import com.ttdj.cmel.adapter.InformationAdapter;
import com.ttdj.cmel.bean.InformationBean;
import com.ttdj.cmel.utils.UmengUtils;
import com.ttdj.cmel.widget.GlideImageLoader;
import com.umeng.analytics.MobclickAgent;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String TAG = "HOMEActivity";
    public static final String SIGNUP_URL = "http://mobile.datangcmel.com/Presentation/Enroll";

    Banner banner;
    View gameBox;
    View cmelSignUp;
    ImageView gameSetting;
    ListView listView;
    List<InformationBean> data = new ArrayList<>();
    //banner data
    String[] bannerUrls;
    List<Integer> bannerImgs = Arrays.asList(R.mipmap.banner_01, R.mipmap.banner_02,
            R.mipmap.banner_03, R.mipmap.banner_04);

    //information data
    String[] informationTitles;
    String[] informationUrl;
    int[] informationImgs = new int[]{R.mipmap.ic_infomation_01, R.mipmap.ic_infomation_02,
            R.mipmap.ic_infomation_03, R.mipmap.ic_infomation_04,
            R.mipmap.ic_infomation_05, R.mipmap.ic_infomation_06};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initViewData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initView() {
        banner = findViewById(R.id.banner);
        gameSetting = findViewById(R.id.btn_game_setting);
        listView = findViewById(R.id.information_list);
        gameBox = findViewById(R.id.game_box);
        gameBox.setOnClickListener(this);
        cmelSignUp = findViewById(R.id.cmel_sign_up);
        cmelSignUp.setOnClickListener(this);
    }

    private void initViewData() {
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        banner.setImages(bannerImgs);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                launchH5(bannerUrls[position]);
                UmengUtils.mainScreenBannerClick("", position + "");
            }
        });
        banner.start();
        gameSetting.setOnClickListener(this);
        data.add(new InformationBean(informationImgs[0], informationTitles[0], informationUrl[0]));
        data.add(new InformationBean(informationImgs[1], informationTitles[1], informationUrl[1]));
        data.add(new InformationBean(informationImgs[2], informationTitles[2], informationUrl[2]));
        data.add(new InformationBean(informationImgs[3], informationTitles[3], informationUrl[3]));
        data.add(new InformationBean(informationImgs[4], informationTitles[4], informationUrl[4]));
        data.add(new InformationBean(informationImgs[5], informationTitles[5], informationUrl[5]));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                launchH5(data.get(position).getUrl());
                UmengUtils.gameNewsClick(data.get(position).getTitle(), "" + position);
            }
        });
        listView.setAdapter(new InformationAdapter(data, this));
    }

    private void initData() {
        //banner data
        bannerUrls = getResources().getStringArray(R.array.banner_url);
        //bannerImgs = getResources().getIntArray(R.array.banner_img);
        //information data
        informationTitles = getResources().getStringArray(R.array.information_title);
        informationUrl = getResources().getStringArray(R.array.information_url);
        //informationImgs = getResources().getIntArray(R.array.information_img);
    }

    private String getStr(int resId) {
        //return Utils.toDBC(getResources().getString(resId));
        return getResources().getString(resId);
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
    }

    private void launchHandlerConfig() {
        Log.d(TAG, "launchHandlerConfig");
        Intent intent = new Intent("com.blackshark.action.GamepadSetting", Uri.parse("bsgamepad://router?from=ttdj"));
        intent.setPackage("com.blackshark.gamelauncher");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_ALL) != null) {
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(getApplicationContext(), "no activity expected", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_game_setting:
                launchHandlerConfig();
                UmengUtils.gameSettingClick();
                break;
            case R.id.game_box:
                launcherApp("com.gzhm.gamebox");
                //launchBrowser("http://bc.iceteapi.com/mobile.php/Game/app_down");
                UmengUtils.hmGameCenterClick();
                break;
            case R.id.cmel_sign_up:
                launchH5(SIGNUP_URL);
                UmengUtils.gameCompetitionClick();
                break;
        }
    }

    public void launcherApp(String pkgName) {
        try {
            Intent intent = getPackageManager().getLaunchIntentForPackage(pkgName);
            if("com.gzhm.gamebox".equals(pkgName) && intent == null) {
                launchBrowser("http://bgcc.blackcore.com.cn/mobile.php/game/app_down");
            } else {
                startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void launchH5(String url) {
        if (url == null || url.isEmpty()) return;
        try {
            Intent intent = new Intent(this, WebViewActivity.class);
            intent.putExtra("web_url", url);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void launchBrowser(String url) {
        if (url == null || url.isEmpty()) return;
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
